﻿using System;
using System.Configuration;
using System.Windows.Forms;

namespace UselessExplorer
{
    public partial class ucPrefrencesExtension : UserControl
    {
        public ucPrefrencesExtension ()
        {
            InitializeComponent();
        }

        private void btnAdd_Click ( object sender, EventArgs e )
        {
            for (int i = 1; i <= 8; i++)
            {
                bool flag = false;
                System.Windows.Forms.Control[] c = this.Controls.Find( "CheckBox" + i, true );
                CheckBox cb = (CheckBox)c[0];

                if (cb.Checked)
                {

                    if (listBox1.Items.Count == 0)
                    {
                        listBox1.Items.Add( cb.Text.ToLower() );
                    }
                    else
                    {
                        foreach (string item in listBox1.Items)
                        {
                            if (item == cb.Text)
                            {
                                flag = true;
                                break;
                            }
                        }

                        if (!flag)
                            listBox1.Items.Add( cb.Text.ToLower() );
                    }
                }
                else if (cb.Checked == false)
                {
                    for (int j = 0; j < listBox1.Items.Count; j++)
                    {

                        if (listBox1.Items[j].ToString() == cb.Text)
                            listBox1.Items.RemoveAt( j );
                    }
                }

            }
            bool flag2 = true;
            if (textBox1.Text != "")
            {
                foreach (string item in listBox1.Items)
                {
                    if (textBox1.Text == item)
                    {
                        flag2 = true;
                        break;
                    }
                    else
                        flag2 = false;
                }
            }

            if (!flag2)
            {
                listBox1.Items.Add( textBox1.Text );
                textBox1.Text = "";
            }
        }

        private void btnSave_Click ( object sender, EventArgs e )
        {
            ClsGlobal cg = ClsGlobal.Instance;
            int i = 0;
            string[] temp = new string[listBox1.Items.Count];
            foreach (string item in listBox1.Items)
            {
                temp[i] = item;
                i++;
            }
            cg.Extensions = temp;

            if (cg.SaveExtensions() == ClsGlobal.StatusExtnAdd.ExtensionAdded)
            {
                toolStripStatusLabel1.Text = UselessExplorer.Properties.UselessResources.ExpAddExtnSuccess;
            }
            else
            {
                toolStripStatusLabel1.Text = UselessExplorer.Properties.UselessResources.ExpAddExtnFailed;
            }

        }

        private void ucPrefrencesExtension_Load ( object sender, EventArgs e )
        {
            toolStripStatusLabel1.Text = "";
            ClsGlobal cg = ClsGlobal.Instance;
            bool flag = true;

            this.Left = cg.FormLeft;
            this.Top = cg.FormTop;

            cg.Extensions = readConfiguration();

            if (cg.Extensions != null)
            {
                string[] temp = cg.Extensions;
                foreach (string item in temp)
                {

                    for (int i = 1; i <= 8; i++)
                    {
                        System.Windows.Forms.Control[] c = this.Controls.Find( "CheckBox" + i, true );
                        CheckBox cb = (CheckBox)c[0];

                        if (item == cb.Text)
                        {
                            cb.Checked = true;
                            listBox1.Items.Add( item );
                            flag = true;
                            break;
                        }
                        else
                            flag = false;
                    }
                    if (!flag)
                        listBox1.Items.Add( item );

                }


            }
        }

        public static string[] readConfiguration ()
        {
            int totalKeys = ConfigurationManager.AppSettings.Keys.Count;
            string[] keys = new string[totalKeys];
            string[] values = new string[totalKeys];
            string[] temp = new string[1];

            keys = ConfigurationManager.AppSettings.AllKeys;

            for (int i = 0; i < keys.Length; i++)
            {
                temp = (ConfigurationManager.AppSettings.GetValues( keys[i] ));
                foreach (string keyValue in temp)
                    values[i] = keyValue;
            }
            return values;
        }

        /// <summary>
        /// Save the Configuration for the Extensions Added. Returns true if added successfully.
        /// </summary>
        /// 
        public ClsGlobal.StatusExtnAdd SaveConfiguration ()
        {
            ClsGlobal.StatusExtnAdd status = ClsGlobal.StatusExtnAdd.Failed;
            string[] extensions = ClsGlobal.Instance.Extensions;
            string keyword = "ext";
            int totalKeys = 0;


            Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None );
            totalKeys = ConfigurationManager.AppSettings.Keys.Count;

            config.AppSettings.Settings.Clear();

            for (int i = 0; i < extensions.Length; i++)
            {
                if (!IsRestricted( extensions[i] ))
                {
                    try
                    {
                        totalKeys = ConfigurationManager.AppSettings.Keys.Count;
                        config.AppSettings.Settings.Add( keyword + (totalKeys), extensions[i] );
                        config.Save( ConfigurationSaveMode.Modified );
                        ConfigurationManager.RefreshSection( "appSettings" );
                        status = ClsGlobal.StatusExtnAdd.ExtensionAdded;
                    }
                    catch (Exception ex)
                    {
                        Logger.Instance.WriteLogs( Logger.LogsCategory.Exception, ex );
                        status = ClsGlobal.StatusExtnAdd.Failed;
                    }
                }
                else
                {
                    status = ClsGlobal.StatusExtnAdd.Restricted;
                }
            }

            return status;
        }

        public bool IsRestricted ( string extension )
        {
            string[] restrictedExtensions = { ".exe", ".dll", ".ico" };
            bool restricted = false;

            foreach (string ext in restrictedExtensions)
            {
                if (ext == extension)
                {
                    restricted = true;
                    break;
                }

            }
            return restricted;
        }

        private bool isDuplicate ( string extension )
        {
            int totalKeys = ConfigurationManager.AppSettings.Keys.Count;
            string keyword = "ext";
            string[] temp = null;
            bool isAvailable = false;

            for (int i = 1; i <= totalKeys; i++)
            {
                temp = ConfigurationManager.AppSettings.GetValues( keyword + i );

                if (temp[0] == extension)
                {
                    isAvailable = true;
                    break;
                }
                else
                    isAvailable = false;
            }
            return isAvailable;
        }





    }
}
